<template>
  <div class="login-container">
    <div class="login-box">                                                                <!--  //阴影  -->
      <div style="padding:50px 30px; background-color:white;margin-left: 100px;border-radius: 5px;box-shadow: 0 0 10px rgba(0,0,0,1)">
        <el-form ref="formRef" :rules="data.rules" :model="data.form" style="width: 350px">
          <div style="margin-bottom: 30px;font-size: 24px;text-align: center;color: #3c7fff;font-weight: bold">欢迎注册</div>
          <el-form-item prop="username" >
            <el-input size="default" v-model="data.form.username" placeholder="请输入账号" prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="email" >
            <el-input size="default" v-model="data.form.email" placeholder="请输入邮箱" prefix-icon=""></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input show-password v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword" >
            <el-input show-password v-model="data.form.confirmPassword" placeholder="请确认密码" prefix-icon="Lock"></el-input>
          </el-form-item>
          <div style="margin-bottom: 20px">
            <el-button @click="register" size="large" style="width: 100%" type="primary">注册</el-button>
          </div>
          <div style="text-align: right">已有账号？请<a style="color: #3c7fff;text-decoration: none" href="/login" >登录</a></div>
        </el-form>
      </div>
    </div>

  </div>
</template>
<script setup >

import {reactive, ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";

const validatePass=(rule , value , callbcak)=>{
  if(!value){
    callbcak(new Error('请再次确认密码'))
  }else if (value!==data.form.password){
    callbcak(new Error("密码不一致"))
  }else{
    callbcak()
  }
}
const data=reactive({
  form:{},
  rules:{
    username:[
      {required:true,message:'请输入账号',trigger:'blur'}
    ],
    no:[
      {required:true,message:'请输入邮箱',trigger:'blur'}
    ],
    password:[
      {required:true,message:'请输入密码',trigger:'blur'}
    ],
    confirmPassword:[
      {validator: validatePass,trigger:'blur'}
    ]
  }
})

const formRef =ref()

const register=()=>{
  formRef.value.validate((valid) => {
    if(valid){
      request.post('/register',data.form).then(res=>{
        console.log(res)
        if(res.code==='200'){
          ElMessage.success('注册成功')
          setTimeout(()=>{
            location.href='/login'
          },500)
        }else{
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

</script>
<style scoped>
.login-container {
  height: 100vh;
  overflow: hidden;
  background-image: url("@/assets/login.jpg");
  background-size: cover;
  background-position: 0,-40px;
}
.login-box {
  width: 50%;
  height: 100%;
  display: flex;
  align-items: center;
  right: 0;
  position: absolute;

}
</style>